
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    cartList: {
      type: Array,
      value: [],
      observer(service) {
        this.getTotalPrice()
        // console.log('service',service);
        // this.setData({
        //   cartList: JSON.parse(JSON.stringify(service))
        // })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // cartList: [],
    selectAll: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getTotalPrice() {
      let totalPrice = 0
      this.data.cartList.map(t => {
        t.cart.map(uu => {
          if (uu.selected && parseFloat(uu.sku.price) && uu.sku.stock > 0 && uu.count > 0) {            
            totalPrice += parseFloat(uu.sku.price) * uu.count
          }
        })
      })

      let newObj = {
        totalPrice: totalPrice.toFixed(2)
        // cartList: JSON.parse(JSON.stringify(this.data.cartList))
      }
      this.triggerEvent('updateCartInfo', newObj)
    },
    checkboxDadClick(e) {
      const {index} = e.currentTarget.dataset
      let arr = JSON.parse(JSON.stringify(this.data.cartList))
      let arrObj = arr[index]
      let dadSelected = arrObj.selected
      let idsArr = []
      arrObj.cart.map(tt => {
        // if (dadSelected) {
        //   arrObj.selected = false
        //   tt.selected = false          
        // } else {
        //   arrObj.selected = true
        //   tt.selected = true
        // }

        idsArr.push(tt.id)
      })

      // this.setData({
      //   cartList: arr
      // }, () => {
      //   this.getTotalPrice()
      // }) 

      const params =  {
        ids: idsArr,
        selected: !dadSelected
      }
      this.triggerEvent('updateCartSelected', params)
    },
    checkboxClick(e) {
      const {cartindex, index} = e.currentTarget.dataset
      let arr = JSON.parse(JSON.stringify(this.data.cartList))
      let obj = arr[cartindex].cart[index]
      // let selected = arr[cartindex].cart[index].selected
      // arr[cartindex].cart[index].selected = !selected
      // // 假值true（实际为false）
      // if (selected) {
      //   arr[cartindex].selected = false
      // } else {
      //   let selectCount = 0 // 记录对应商品下每个商品被选中的计数器
      //   arr[cartindex].cart.map(yy => {
      //     if (yy.selected) {
      //       selectCount++
      //     }
      //   })

      //   if (selectCount > 0 && selectCount === arr[cartindex].cart.length) {
      //     arr[cartindex].selected = true
      //   }
      // }
      // this.setData({
      //   cartList: arr
      // }, () => {
      //   this.getTotalPrice()
      // }) 

      const params = {
        ids: [].concat(obj.id),
        selected: !obj.selected
      }
      this.triggerEvent('updateCartSelected', params)
    },
    stepperChange(e) {
      let {cartindex, index} = e.currentTarget.dataset
      let arr = JSON.parse(JSON.stringify(this.data.cartList))
      // arr[cartindex].cart[index].count = e.detail.value
      // this.setData({
      //   cartList: arr
      // }, () => {
      //   this.getTotalPrice()
      // })      

      const params = {
        id: arr[cartindex].cart[index].id,
        count: e.detail.value
      }
      this.triggerEvent('updateCartCount', params)
    }
  }
})